/******************************************************************************
	This file is a part of the Snapshot Bioinformatics Project

	Copyright (C) 2012-2013 Jarom Schow

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include "Core.h"

#include <QtCore/QDebug>

#include "MessageHandler.h"

using namespace Bio;

/**
	Static data class to manage the life of the message handler.
**/
class BioData {
public:
	BioData() : m_msgHandler( NULL ) { }
	~BioData() 
	{ 
		delete m_msgHandler; m_msgHandler = NULL;
	}

	MessageHandler		*m_msgHandler;
};

static BioData	s_data;

///////////////////////////////////////////////////////////////////////////////
//	Core
///////////////////////////////////////////////////////////////////////////////

/**
	@class Core
	@brief A central utility class for the Bio library.

	The Core class provides message handling for the Bio library. All messages in the library are
	passed through this class, and applications using the library may also use this class for 
	messaging. All the messages are passed to the current message handler if one is set. If a message
	handler is not set, the messages are passed to the qDebug() function.
**/

/**
	This function can be called to pass an error message to the message handler.
	@param	msg	The error message to pass.
**/
void Core::error( const QString &msg )
{
	if( s_data.m_msgHandler )
		s_data.m_msgHandler->error( msg );
	else
		qDebug() << msg;
}

/**
	This function can be called to pass a warning message to the message handler.
	@param	msg	The warning message to pass.
**/
void Core::warning( const QString &msg )
{
	if( s_data.m_msgHandler )
		s_data.m_msgHandler->warning( msg );
	else
		qDebug() << msg;
}

/**
	This function can be called to pass a general log message to the message handler.
	@param	msg	The message to log.
**/
void Core::log( const QString &msg )
{
	if( s_data.m_msgHandler )
		s_data.m_msgHandler->log( msg );
	else
		qDebug() << msg;
}

/**
	Calls flush() on the current message handler if one is set. The underlying behavior
	depends on the message handler, but this may flush any buffered messages to a file,
	for example.
**/
void Core::flushLog()
{
	if( s_data.m_msgHandler )
		s_data.m_msgHandler->flush();
}

/**
	Sets the handler that will recieve all the messages passed. Applications can create 
	their own message handler and set it using this function to capture, handle, log, etc, 
	all the messages from the Bio library.
**/
void Core::setMessageHandler( MessageHandler *msgHandler )
{
	delete s_data.m_msgHandler;
	s_data.m_msgHandler = msgHandler;
}
